import threading
from datetime import datetime

from django.http import HttpResponse

from ._builtin import Page, WaitPage

from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render,redirect
import settings
from datetime import datetime
from threading import Timer
import os
import time
# #TODO 提交表单 数据检查
@csrf_exempt
def addDemo(request):
    # print("发送数据")
    name = request.POST.get('uname')
    display_name = request.POST.get('uname')
    num_demo_participants = request.POST.get('ndp')
    plf1 = request.POST.get('plf1')
    plf2 = request.POST.get('plf2')
    plf3 = request.POST.get('plf3')
    plf4 = request.POST.get('plf4')
    plf5 = request.POST.get('plf5')
    plf6 = request.POST.get('plf6')
    plf7 = request.POST.get('plf7')
    print(plf1)
    print(plf2)
    print(plf3)
    print(plf4)
    print(plf5)
    print(plf6)
    print(plf7)
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    # print("现在在这里")
    app_sequence=[plf1,plf2,plf3,plf4,plf5,plf6,plf7]
    # 向settings中添加
    addnewdata(name, display_name, num_demo_participants, app_sequence)
    # return
    print("执行函数adddemo")
    return HttpResponse('提交成功！')
    # return redirect()

def addnewdata(name,display_name,num_demo_participants,app_sequence):

    fo_r = open("../oTree/settings.py", "r",encoding='UTF-8')
    content = fo_r.read()
    fo_r.close()
    #将数组拼接成字符串
    app_sequence_link=""
    for a_s in app_sequence:
        if a_s== None :
            break;
            print("以中断")
        if a_s == "none":
            break;
            print("以中断")
        app_sequence_link = app_sequence_link + "\'" + a_s + "\'" + ", "

    pos = content.find("SESSION_CONFIGS = [")
    data1 = "\tdict("
    data2 = "\t\tname="+ "\'"+name+ "\'"+ ","
    data3 = "\t\tdisplay_name="+ "\""+display_name+ "\""+ ","
    data4 = "\t\tnum_demo_participants="+num_demo_participants+ ","
    data5 = "\t\tapp_sequence="+ "["+app_sequence_link+ "]"
    data6 = "\t),"

    # data1 = "    dict("
    # data2 = "        name="+ "\'"+name+ "\'"+ ","
    # data3 = "        display_name="+ "\""+display_name+ "\""+ ","
    # data4 = "        num_demo_participants="+num_demo_participants+ ","
    # data5 = "        app_sequence="+ "["+app_sequence_link+ "]"
    # data6 = "    ),"
    #格式和虚拟机不一样 在虚拟机用这个格式
    if (pos != -1):
        pos = pos + 19
        content = content[:pos] + "\n" + data1 + "\n" + data2 + "\n" + data3 + "\n" + data4 + "\n" + data5 + "\n" + data6 + "\n" + content[pos:]

        fo_w = open("settings.py", "w")
        fo_w.write(content)
        fo_w.close()
    # 5s
    # timer = threading.Timer(10, restart)
    # print("服务器即将于10s后关闭")
    # timer.start()

@csrf_exempt
def reload():
    print("准备重启。")
    os.popen("killall -9 otree")


@csrf_exempt
def AddDemoWaiting(request):

    print("执行函数AddDemoWaiting")
    print("准备重启。")

    timer = threading.Timer(3,restart)
    print("服务器即将于3s后关闭")
    timer.start()
    # time.sleep(99)
    return redirect("/demo")

def restart():
    os.popen("killall -9 otree")

class MyPage(Page):
    pass



# class ResultsWaitPage(WaitPage):
#     pass
#
#
# class Results(Page):
#     pass



# page_sequence = [MyPage, ResultsWaitPage, Results]
page_sequence = [MyPage]
